package com.hxx.day7.simple.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.hxx.day7.simple.domain.User;
import com.hxx.day7.simple.service.UserService;

public class UserServiceImpl implements UserService {
    //模拟数据库
    private List list = new ArrayList();

    @Override
    public void save(User user) {
        if (user!= null) {
            list.add(user);
            System.out.println("新增用户成功");
        }
    }

    @Override
    public User findById(Integer id) {
        if (list.size()>0){
            for (Object o : list) {
                //转换
                User u = (User) o;
                if (u.getId().equals(id)){
                    return u;
                }
            }
        }
        return null;
    }

    @Override
    public void update(User user) {
        if (user!=null){
            User user1 = findById(user.getId());
            if (user1!=null){
                user1.setUsername(user.getUsername());
                user1.setAge(user.getAge());
                System.out.println("更新用户成功");
            }
        }
    }

    @Override
    public void delete(Integer id) {
        if (id!=null){
            User user = findById(id);
            if (user!=null){
                list.remove(user);
                System.out.println("删除用户成功");
            }else {
                System.out.println("删除用户失败");
            }
        }
    }

    @Override
    public List<User> findAll() {
        return list;
    }
}
